<?php
session_start();
/*
 * Session variables:
 * $_SESSION['type'] [string] = user type of the logged in user
 * $_SESSION['authorised'] [boolean] = whether or not the current user is logged in
 * $_SESSION['student_pass'] [string]= password of student who arrived
 */
if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{   
    if(isset($_SESSION['type']))
    {
        if($_SESSION['type'] == 'administrator')
        {
			include ('connection.inc');
			$class_id = $_SESSION['addstudent_class'];
			$student_id = $_POST['student'];
			 $sql = "Select * from enrolled_at where class_id='$class_id' AND student_id='$student_id'";
			 $result = pg_query($sql);
			 $count = pg_num_rows($result);
			 if($count<1)
			 {
			 	$sql = "Insert into enrolled_at (class_id, student_id) Values ('$class_id', '$student_id')";
			 	$result = pg_query($sql);
				$sql = "update classes set num_students=num_students+1 where class_id='$class_id'";
			 	$result = pg_query($sql);
			 }
			 
	        	header('Location:admin_viewclasses.php');
	        	exit();
        }
        elseif($_SESSION['type'] == 'student')
        {
            header('Location: student_index.php');
            exit();
        }
        elseif($_SESSION['type'] == 'tutor')
        {
            header('Location: tutor_index.php');
            exit();
        }
        elseif($_SESSION['type'] == 'parent')
        {
            header('Location: parent_index.php');
            exit();
        }
    }
}
else
{
    header('Location: index.php');
}
?>
